package com.example.shushiyunsst.scan

import android.Manifest
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.example.library_common.album.constant.AlbumErrorMode
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.LazyFragment
import com.example.shushiyunsst.R
import com.example.shushiyunsst.util.extension.goneAlphaAnimation
import com.example.shushiyunsst.util.extension.visibleAlphaAnimation
import com.permissionx.guolindev.PermissionX

class ScanFrameFragment : BaseFragmentEx() {

    companion object {
        fun newInstance(): ScanFrameFragment {
            val args = Bundle()
            val fragment = ScanFrameFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var mScanResListener: ScanResListener? = null

    private var mViewLoading: View? = null

    private var frameLayout:View? = null

    override fun getLayoutId() = R.layout.scan_frame_fragment

    fun setScanResListener(mScanResListener: ScanResListener?) {
        this.mScanResListener = mScanResListener
    }

    override fun onEnterAnimationEnd(savedInstanceState: Bundle?) {
        super.onEnterAnimationEnd(savedInstanceState)
        PermissionX.init(this)
            .permissions(Manifest.permission.CAMERA)
            .onExplainRequestReason { scope, deniedList ->
                val msg = "获取内部存储和摄像头权限"
                val positive = "确定"
                val negative = "取消"
                scope.showRequestReasonDialog(deniedList, msg, positive, negative)
            }.onForwardToSettings { scope, deniedList ->
                val msg = "获取内部存储和摄像头权限"
                val positive = "确定"
                val negative = "取消"
                scope.showForwardToSettingsDialog(deniedList, msg, positive, negative)
            }.request { allGranted, _, deniedList ->
                if (!allGranted || deniedList.isNotEmpty()) {

                    this.onBackPressedSupport()
                } else {
                    loadSan()
                }

            }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewLoading = view.findViewById(R.id.mViewLoading)
        frameLayout=view.findViewById(R.id.frameLayout)
//        PermissionX.init(this)
//            .permissions(Manifest.permission.CAMERA)
//            .onExplainRequestReason { scope, deniedList ->
//                val msg = "获取内部存储和摄像头权限"
//                val positive = "确定"
//                val negative = "取消"
//                scope.showRequestReasonDialog(deniedList, msg, positive, negative)
//            }.onForwardToSettings { scope, deniedList ->
//                val msg = "获取内部存储和摄像头权限"
//                val positive = "确定"
//                val negative = "取消"
//                scope.showForwardToSettingsDialog(deniedList, msg, positive, negative)
//            }.request { allGranted, _, deniedList ->
//                if (!allGranted || deniedList.isNotEmpty()) {
//
//                    this.onBackPressedSupport()
//                } else {
//                    loadSan()
//                }
//
//            }
    }


    private fun loadSan() {
        frameLayout?.visibleAlphaAnimation()
        mViewLoading?.goneAlphaAnimation()
        val newInstance = ScanFragment.newInstance()
        newInstance.setScanResListener(mScanResListener)
        loadRootFragment(R.id.frameLayout, newInstance)
    }


}